﻿//配置插件目录
layui.config({
    base: '/mods/'
});

layui.use(['form', 'laydate', 'util', 'jquery', 'layer', 'table', 'tree'], function () {
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var $ = layui.jquery;
    var tree = layui.tree;
    //弹出层
    $('#onAddinfo').on('click', function () {
        // 批量事件
        layer.open({
            type: 1, // page 层类型
            area: ['1200px', '800px'],
            title: '新增事件类型',
            shade: 0,//控制遮罩
            shadeClose: true, // 点击遮罩区域，关闭弹层
            maxmin: true, // 允许全屏最小化
            anim: 0, // 0-6 的动画形式，-1 不开启
            content: $("#add-main"),
        });
    });
    $(function () {
        EventSelect();
    })
    function EventSelect() {
        $.ajax({
            url: "http://8.140.245.229:8000/routechen/api/Form/HandleFormSelect?PId=0",
            async: false,
            type: "get",
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            success: function (res) {
                var select = $("#TypeName");
                $.each(res, function (index, item) {
                    select.append($('<option>').val(item.EventTypeId).text(item.EventTypeName))
                })
                layui.form.render('select');
                form.on('select(eventtype)', function (data) {
                    var value = data.value; // 获得被选中的值
                    $.ajax({
                        url: "http://8.140.245.229:8000/routechen/api/Form/HandleFormBigClass",
                        data: {
                            PId: value
                        },
                        type: "get",
                        headers: {
                            'Authorization': "Bearer " + localStorage["token"] + ""
                        },
                        success: function (res) {
                            var select = $("#TypeFther");
                            $("#TypeFther").empty();
                            select.append($('<option>').val(0).text("请选择"))
                            $.each(res, function (index, item) {
                                select.append($('<option>').val(item.EventTypeName).text(item.EventTypeName))
                            })
                            layui.form.render('select');
                            form.on('select(eventbig)', function (data) {
                                var value = data.value; // 获得被选中的值
                                $.ajax({
                                    url: "http://8.140.245.229:8000/routechen/api/Form/HandleSmallClass",
                                    data: {
                                        PId: value
                                    },
                                    type: "get",
                                    headers: {
                                        'Authorization': "Bearer " + localStorage["token"] + ""
                                    },
                                    success: function (res) {
                                        var select = $("#TypeSun");
                                        $("#TypeSun").empty();
                                        $.each(res, function (index, item) {
                                            select.append($('<option>').val(item.TypeName).text(item.TypeName))
                                        })
                                        layui.form.render('select');
                                    }
                                })
                            })
                        }
                    })
                });
            }
        })
    }
    function AddLogInfo(OperationalContext, PreOperation, AfterOperation) {
        const data = {
            olContext: OperationalContext,
            PreOperation: PreOperation,
            AfterOperation: AfterOperation
        };
        // alert(JSON.stringify(data));
        $.ajax({
            url: "http://8.140.245.229:8000/WorkBench/api/Log/AddLog",
            type: 'post',
            dataType: 'json',
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            contentType: 'application/json', // 添加正确的 Content-Type 头部
            data: JSON.stringify(data),
            success: function (res) {
                // alert(res)
                getListData(1);
                console.log(res)
            }
        })
    }

    form.on('submit(demo1)', function (data) {
        var obj = {};
        var field = data.field;
        obj.FormName = field.FormName;
        obj.FromDesc = field.FromDesc;
        obj.TypeName = field.TypeName;
        obj.TypeFther = field.TypeFther;
        obj.TypeSun = field.TypeSun;
        obj.CreateBy = field.CreateBy;
        obj.CreateTime = field.CreateTime;
        obj.UpdateBy = null;
        obj.UpdateTime = null;
        obj.IsDel = false;
        $.ajax({
            url: "http://8.140.245.229:8000/webroute/api/Setting/HandleAddForm",
            type: "post",
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            data: JSON.stringify(obj),
            contentType: "application/json",
            success: res => {
                AddLogInfo("向表单管理添加数据", "", JSON.stringify(obj))
                if (res > 0) {
                    layer.confirm('添加成功', function (index) {
                        ShowInfo(1);
                    });

                } else {
                    layer.alert("添加失败");
                }
            }
        })
        return false; // 阻止默认 form 跳转
    })
})